package com.coollang.smartbra.views;

import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import com.coollang.smartbra.R;
import com.coollang.smartbra.utils.DisplayUtils;


public class DeviceBetrryCircleProgress extends View{
	private Context mContext;
	private Paint paint;

	/**
	 * 圆环的颜色
	 */
	private int roundColor;
	/**
	 * 圆环进度的颜色
	 */
	private int roundProgressColor;
	/**
	 * 圆环的宽度
	 */
	private float roundWidth;

	/**
	 * 最大进度
	 */
	private int maxCount;

	/**
	 * 当前进度
	 */
	private int curCount;
	/**
	 * 整个圆环的宽
	 */
	private int rWidth;
	/**
	 * 整个圆环的高
	 */
	private int rHeight;
	/**
	 * 顶层透明覆盖膜
	 */
	private int mTranslate;
	/**
	 * 缓动动画
	 */
	private ValueAnimator backTopAni;
	/**
	 * 进度条是否圆角
	 */
	private boolean isRound;

	private Float fromFloat = 0f;
	
	private Float scoreint;
	public DeviceBetrryCircleProgress(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		mContext = context;
		paint = new Paint();

		rWidth = DisplayUtils.dip2px(context, 190);
		rHeight = DisplayUtils.dip2px(context, 190);
		roundColor = attrs.getAttributeResourceValue(null, "roundColor", R.color.white);
		roundWidth = 18;
		roundProgressColor = attrs.getAttributeResourceValue(null, "roundProgressColor",R.color.search_finish_tv_color);
		
		mTranslate = attrs.getAttributeResourceValue(null, "mTranslate",R.color.white);
		curCount = 0;
		maxCount = 100;
		scoreint = (float) (curCount/maxCount);
		init(context, attrs, defStyleAttr);
	}

	private void init(Context context, AttributeSet attrs, int defStyleAttr) {
		// TODO Auto-generated method stub
		TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.DeviceBetrryCircleProgress);
		isRound = a.getBoolean(R.styleable.DeviceBetrryCircleProgress_isRound, false);
		a.recycle();
	}

	public DeviceBetrryCircleProgress(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public DeviceBetrryCircleProgress(Context context) {
		this(context, null);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		/**
		 * 画最外层的大圆环
		 */
		int centre = rWidth / 2; // 获取圆心的x坐标
		int radius = (int) (centre - roundWidth / 2); // 圆环的半径
		paint.setColor(mContext.getResources().getColor(roundColor)); // 设置圆环的颜色
		paint.setStyle(Paint.Style.STROKE); // 设置空心
		paint.setStrokeWidth(roundWidth); // 设置圆环的宽度
		paint.setAntiAlias(true); // 消除锯齿
		canvas.drawCircle(centre, centre, radius, paint); // 画出圆环

		/**
		 * 画圆弧 ，画圆环的进度
		 */

		// 设置进度是实心还是空心
		paint.setStrokeWidth(roundWidth); // 设置圆环的宽度
		paint.setColor(mContext.getResources().getColor(roundProgressColor)); // 设置进度的颜色
		RectF oval = new RectF(centre - radius, centre - radius, centre + radius, centre + radius); // 用于定义的圆弧的形状和大小的界限
		paint.setAntiAlias(true); // 消除锯齿
		paint.setStyle(Paint.Style.STROKE);
		if(isRound){		
			paint.setStrokeCap(Cap.ROUND);
		}
		float jindu = 360 * scoreint;
		if (jindu == 0) {
			jindu = 1;
		}
		canvas.drawArc(oval, -90, jindu, false, paint); // 根据进度画圆弧
		fromFloat = scoreint;
		/**
		 * 画最顶层的透明薄膜
		 */
		paint.setColor(mContext.getResources().getColor(mTranslate)); // 设置圆环的颜色
		paint.setStyle(Paint.Style.STROKE); // 设置空心
		paint.setStrokeWidth(roundWidth-3); // 设置圆环的宽度
		paint.setAntiAlias(true); // 消除锯齿
		canvas.drawCircle(centre, centre, radius+2, paint); // 画出圆环

	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		int widthSize = MeasureSpec.getSize(widthMeasureSpec);
		int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		int heightSize = MeasureSpec.getSize(heightMeasureSpec);
		int width;
		int height;
		if (widthMode == MeasureSpec.EXACTLY) {
			width = widthSize;
		} else {
			width = rWidth;
		}

		if (heightMode == MeasureSpec.EXACTLY) {
			height = heightSize;
		} else {
			height = rHeight;
		}
		setMeasuredDimension(width, height);
	}

	public int getMaxCount() {
		return maxCount;
	}

	public void setMaxCount(int maxCount) {
		this.maxCount = maxCount;
	}

	public int getCurCount() {
		return curCount;
	}

	public void setCurCount(int curCount, int maxCount) {
		this.maxCount = maxCount;
		this.curCount = curCount;
		setAnim(fromFloat, (float) curCount / maxCount);
	}
	
	@SuppressLint("NewApi")
	private void setAnim(float from, float to) {
		backTopAni = ValueAnimator.ofFloat(from, to);
		backTopAni.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				scoreint = (Float) animation.getAnimatedValue();
				postInvalidate();
			}
		});
		
		backTopAni.setInterpolator(new DecelerateInterpolator(3));
		backTopAni.setDuration(2000);
		backTopAni.start();

	}

}
